/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.workload;

import org.nudt.jCloud.fileSystem.Block;
import org.nudt.jCloud.infrastructure.Slot;
import org.nudt.jCloud.simulation.VirtSlot;

public class Task {
	private int id;
	private double duration;
	private Slot slot;
	private Block block;
	private Job job;

	public Task(int id, double duration) {
		setId(id);
		setDuration(duration);
		setSlot(null);
		setBlock(null);
	}
	
	public void assignToSlot(Slot slot){
		if(getJob().getUnScheduledTasks().contains(this)==false){
			System.out.println("UserBroker:"+this.getJob().getXJob().getWl().getUser().getUserBroker().getId()+
					"Job: "+this.getJob().getXJob().getId()+"task: "+this.getId());
			throw new RuntimeException("cannot assign this task.");
		}else{
			setSlot(slot);
			getJob().getUnScheduledTasks().remove(this);
			getJob().getScheduledTasks().add(this);			
		}
	}
	
	public void assignToVirtSlot(VirtSlot virtSlot) {
		if(getJob().getUnScheduledTasks().contains(this)==false){
			throw new RuntimeException("cannot assign this task.");
		}else{
			getJob().getUnScheduledTasks().remove(this);
			getJob().getScheduledTasks().add(this);
		}
	}
	
	public void cancelAssignToVirtSlot(){
		if(getJob().getScheduledTasks().contains(this)==false){
			throw new RuntimeException("cannot cancel this task.");
		}else{
			getJob().getScheduledTasks().remove(this);
			getJob().getUnScheduledTasks().add(this);
		}
	}
	
	public void cancelAssignToSlot(){
		if(getJob().getScheduledTasks().contains(this)==false){
			throw new RuntimeException("cannot cancel this task.");
		}else{
			getJob().getScheduledTasks().remove(this);
			getJob().getUnScheduledTasks().add(this);
		}
	}

	public boolean locality(){
		if(block == null){
			return true;
		}else{
			return block.locality(slot);
		}
	}
	

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	
	public Slot getSlot() {
		return slot;
	}

	public void setSlot(Slot slot) {
		this.slot = slot;
	}

	public double getDuration() {
		return duration;
	}

	public void setDuration(double duration) {
		this.duration = duration;
	}

	public Block getBlock() {
		return block;
	}

	public void setBlock(Block block) {
		this.block = block;
	}


	public Job getJob() {
		return job;
	}


	public void setJob(Job job) {
		this.job = job;
	}
	
}
